package com.hencoder.hencoderpracticedraw1.practice;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

public class Practice10HistogramView extends View {
    private final String Tag = getClass().getSimpleName();
    private Paint paint = null;
    private int defaultMargin = 80;
    private int defaultSpace = 10;
    private String[] titleString = {"Froyo", "GB", "ICS", "JB", "KitKat", "L", "M"};
    private ArrayList<String> titles = new ArrayList<String>(Arrays.asList(titleString));
    private double[] vauleString = {1, 5, 5, 50, 70, 80, 38};
    public Practice10HistogramView(Context context) {
        super(context);
        init();
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public Practice10HistogramView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }
    private void init() {
        paint = new Paint();
        paint.setStyle(Paint.Style.FILL);
        paint.setTextAlign(Paint.Align.CENTER);
        paint.setAntiAlias(true);
    }
    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);

//        综合练习
//        练习内容：使用各种 Canvas.drawXXX() 方法画直方图
        int mWidth = getMeasuredWidth();
        int mHeight = getMeasuredHeight();
        /*控件宽度减去间隔数等于chart有效宽度*/
        int chartWidth = (int) (mWidth - defaultMargin *2 - (titles.size() + 1) * defaultSpace);
        int chartHeight = (int) (mHeight - defaultMargin * 3 / 2);
        /*chart有效宽度/数量 等于 每个列的宽度*/
        int columnWidth = chartWidth / titles.size();

        canvas.drawColor(Color.GRAY);
        paint.setColor(Color.WHITE);
        /*绘制Y轴*/
        canvas.drawLine(defaultMargin, defaultMargin / 2, defaultMargin, mHeight - defaultMargin * 3 / 2, paint);
        /*绘制X轴*/
        canvas.drawLine(defaultMargin, mHeight - defaultMargin * 3 / 2, mWidth - defaultMargin, mHeight - defaultMargin * 3 / 2, paint);
        /*绘制垂直柱子*/
//        canvas.drawRect(defaultMargin + defaultSpace, chartHeight - defaultSpace, defaultMargin + defaultSpace, defaultMargin + defaultSpace + columnWidth, paint);
        for (int i = 0; i < titles.size(); i++) {
            int left = defaultMargin + (i + 1) * defaultSpace + columnWidth * i;
            double topgapPercent = Double.valueOf(formatDouble(vauleString[i] / 100));
            Log.d(Tag, "topgapPercent" + topgapPercent);
            Log.d(Tag, "chartHeight * topgapPercent=" + chartHeight * topgapPercent);
            float top = (float) (chartHeight - chartHeight * topgapPercent);
            int right = defaultMargin + (i + 1) * defaultSpace + columnWidth * (i + 1);
            int bottom = chartHeight;
            Log.d(Tag, "left=" + left);
            Log.d(Tag, "top=" + top);
            Log.d(Tag, "right=" + right);
            Log.d(Tag, "bottom=" + bottom);
            paint.setColor(Color.GREEN);
            canvas.drawRect(left,(float) top,right,bottom, paint);
            /*绘制垂直柱子label*/
            paint.setColor(Color.WHITE);
            paint.setTextSize(20);
            /*绘制column的value*/
            canvas.drawText(String.valueOf(vauleString[i]), left + columnWidth / 2, top - defaultSpace * 2, paint);
            /*绘制column的label*/
            canvas.drawText(titleString[i], left + columnWidth / 2, bottom + defaultSpace * 2, paint);

        }
        /*绘制X轴底部文字*/
        paint.setColor(Color.WHITE);
        paint.setTextSize(30);
        canvas.drawText("直方图", mWidth / 2, mHeight - 30, paint);
    }
    public static String formatDouble(double d) {
        return String.format("%.2f", d);
    }
}
